package com.liveneo.adapter.webservice.server.claim.model.dto;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import com.liveneo.adapter.base.support.LogUtils;
import com.liveneo.adapter.mobile.model.dto.VsRequestHeadDto;
import com.liveneo.adapter.webservice.server.claim.Exception_Exception;
import com.liveneo.adapter.webservice.server.claim.model.Claim2VsRequest;
import com.liveneo.adapter.webservice.server.claim.model.Claim2VsResponse;
import com.liveneo.adapter.webservice.server.claim.service.Claim2VsFacade;
import com.liveneo.adapter.webservice.server.claim.service.Claim2VsServiceService;
import com.liveneo.system.base.utils.MD5;
import com.thoughtworks.xstream.XStream;

/**
 *@Title:
 *@Description:  和核心理赔接口统一处理类
 *@Author:YJX
 *@Since:2015-8-20 上午09:31:47
 *@Version:1.0.0
 *@Copyright (c) 北京朗泰恒盛信息技术有限公司-版权所有
 */
public class Claim2VsServiceManage {
    private Claim2VsServiceService          claim2VsServiceService = new Claim2VsServiceService();
    private Claim2VsFacade                  claim2VsFacade         = claim2VsServiceService.getClaim2VsServicePort();
    private XStream                         xs                     = new XStream();
    private Claim2VsResponse                response;
    private UUID                            uuid                   = null;                                           // 每次交易的唯一标识符
    /**
     * 请求类型定义
     * P03-findEvaluateInfo-定损单信息查询接口
     * P04-upoadImage-影像资料上传接口
     * P05-confirmEvaluate-用户定损结果确认接口
     * P06-setNormalRegisterService-置为正常案件
     * P07-normalEndService-案件正常结束
     * P08-applySurveyDelegate-查勘员模式调度
     * P09-applySurveyReassign-查勘员模式改派
     * P10-surveyMembersReassign-查勘员模式查勘员改派
     * P11-uploadDocumentImage-单证补传
     * P12-findCaseInfo-案件信息查询
     * */
    public static final Map<String, String> REQUEST_TYPE_MAP       = new HashMap<String, String>();
    static {
        REQUEST_TYPE_MAP.put("P03", "findEvaluateInfo");
        REQUEST_TYPE_MAP.put("P04", "upoadImage");
        REQUEST_TYPE_MAP.put("P05", "confirmEvaluate");
        REQUEST_TYPE_MAP.put("P06", "setNormalRegisterService");
        REQUEST_TYPE_MAP.put("P07", "normalEndService");
        REQUEST_TYPE_MAP.put("P08", "applySurveyDelegate");
        REQUEST_TYPE_MAP.put("P09", "applySurveyReassign");
        REQUEST_TYPE_MAP.put("P10", "surveyMembersReassign");
        REQUEST_TYPE_MAP.put("P11", "uploadDocumentImage");
        REQUEST_TYPE_MAP.put("P12", "findCaseInfo");
        REQUEST_TYPE_MAP.put("P15", "synBranchInfo");
        REQUEST_TYPE_MAP.put("P16", "uploadOCRinfo2JY2G");
    }

    /**
     * @Description:  和核心理赔交互的入口方法
     * @param request  请求对象
     * @param strRequestType  请求类型
     * 
     * @return   
     * @ReturnType:Claim2VsResponse 返回对象
     * @exception 
     * @Since:2015-8-20 上午09:33:56
     * @Author:YJX
    */
    public Claim2VsResponse getResponse(Claim2VsRequest request, String strRequestType, String userCode) {
        // 封装对象头信息
        uuid = java.util.UUID.randomUUID();
        setRequestHead(request, strRequestType, userCode);
        if (!"P04".equals(strRequestType) && !"P11".equals(strRequestType)) {// 上传影像到理赔时，入参不打印
            LogUtils.info("发送信息-" + strRequestType + "-" + uuid + "：" + xs.toXML(request));
        }
        Method method;
        try {
            method = claim2VsFacade.getClass().getMethod(REQUEST_TYPE_MAP.get(strRequestType).toString(), Claim2VsRequest.class);
            response = (Claim2VsResponse) method.invoke(claim2VsFacade, request);
            LogUtils.info("返回信息-" + strRequestType + "-" + uuid + "：" + "*************" + xs.toXML(response));
        } catch (java.lang.Exception e) {
            e.printStackTrace();
        }
        return response;
    }

    /**
     * @Description: 封装发送对象的头信息
     * @param request  请求对象
     * @param strRequestType   请求类型
     * @ReturnType:void
     * @exception 
     * @Since:2015-8-20 上午10:05:06
     * @Author:YJX
    */
    private void setRequestHead(Claim2VsRequest request, String strRequestType, String userCode) {
        VsRequestHeadDto requestHeadDto = new VsRequestHeadDto();
        requestHeadDto.setClientCode("msplatform");
        requestHeadDto.setClientName("移动视频查勘平台");
        requestHeadDto.setOperation(REQUEST_TYPE_MAP.get(strRequestType).toString());
        requestHeadDto.setPassword(MD5.GetMD5Code("QO73*3%mms"));
        requestHeadDto.setRequestID(uuid.toString());
        requestHeadDto.setServerCode("002");
        requestHeadDto.setServiceCode("VideoSurveyService");
        requestHeadDto.setServiceName("车险视频查勘定损系统");
        requestHeadDto.setUserCode(userCode);
        requestHeadDto.setUserName("");
        requestHeadDto.setUsrSignature("");
        request.setVsRequestHeadDto(requestHeadDto);
    }

    @SuppressWarnings("unused")
    public static void main(String[] args) throws Exception_Exception {
        Claim2VsServiceManage claim2VsServiceManage = new Claim2VsServiceManage();
        Claim2VsRequest request = new Claim2VsRequest();
        request.setEvaluateOpinionRequestDto(new EvaluateOpinionRequestDto());
        request.getEvaluateOpinionRequestDto().setRegistNo("12121121212");
        request.getEvaluateOpinionRequestDto().setLossId("121");
        Claim2VsResponse response = claim2VsServiceManage.getResponse(request, "P03", "");
    }
}
